package com.bugsnag.ohos;

import java.io.IOException;
import java.util.Map;

/**
 * Stateless information set by the notifier about the device on which the event occurred can be
 * found on this class. These values can be accessed and amended if necessary.
 *
 * @since 2021-07-06
 */
public class Device implements JsonStream.Streamable {
    /**
     * build info
     */
    private DeviceBuildInfo buildInfo;

    /**
     * The Application Binary Interface used
     */
    private String[] cpuAbi;

    /**
     * Whether the device has been jailbroken
     */
    private boolean jailbroken;

    /**
     * A UUID generated by Bugsnag and used for the individual application on a device
     */
    private String id;

    /**
     * The IETF language tag of the locale used
     */
    private String locale;

    /**
     * The total number of bytes of memory on the device
     */
    private long totalMemory;

    /**
     * A collection of names and their versions of the primary languages, frameworks or
     * runtimes that the application is running on
     */
    private Map<String, Object> runtimeVersions;

    /**
     * The manufacturer of the device used
     */
    private String manufacturer;

    /**
     * The model name of the device used
     */
    private String model;

    /**
     * The name of the operating system running on the device used
     */
    private String osName = "ohos";

    /**
     * The version of the operating system running on the device used
     */
    private String osVersion;

    /**
     * constructor
     *
     * @param buildInfo
     * @param cpuAbi
     * @param jailbroken
     * @param id
     * @param locale
     * @param totalMemory
     * @param runtimeVersions
     */
    public Device(DeviceBuildInfo buildInfo, String[] cpuAbi, boolean jailbroken,
                  String id, String locale, long totalMemory, Map<String, Object> runtimeVersions) {
        this.buildInfo = buildInfo;
        this.cpuAbi = cpuAbi;
        this.jailbroken = jailbroken;
        this.id = id;
        this.locale = locale;
        this.totalMemory = totalMemory;
        this.runtimeVersions = runtimeVersions;
        manufacturer = buildInfo.manufacturer;
        model = buildInfo.model;
        osVersion = buildInfo.osVersion;
    }

    public DeviceBuildInfo getBuildInfo() {
        return buildInfo;
    }

    public void setBuildInfo(DeviceBuildInfo buildInfo) {
        this.buildInfo = buildInfo;
    }

    public String[] getCpuAbi() {
        return cpuAbi;
    }

    public void setCpuAbi(String[] cpuAbi) {
        this.cpuAbi = cpuAbi;
    }

    public boolean getJailbroken() {
        return jailbroken;
    }

    public void setJailbroken(boolean jailbroken) {
        this.jailbroken = jailbroken;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLocale() {
        return locale;
    }

    public void setLocale(String locale) {
        this.locale = locale;
    }

    public long getTotalMemory() {
        return totalMemory;
    }

    public void setTotalMemory(long totalMemory) {
        this.totalMemory = totalMemory;
    }

    public Map<String, Object> getRuntimeVersions() {
        return runtimeVersions;
    }

    public void setRuntimeVersions(Map<String, Object> runtimeVersions) {
        this.runtimeVersions = runtimeVersions;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getOsName() {
        return osName;
    }

    public void setOsName(String osName) {
        this.osName = osName;
    }

    public String getOsVersion() {
        return osVersion;
    }

    public void setOsVersion(String osVersion) {
        this.osVersion = osVersion;
    }

    @Override
    public void toStream(JsonStream writer) throws IOException {
        writer.beginObject();
        serializeFields(writer);
        writer.endObject();
    }

    /**
     * serialize fields
     *
     * @param writer
     * @throws IOException
     */
    public void serializeFields(JsonStream writer) throws IOException {
        writer.name("cpuAbi").value(cpuAbi);
        writer.name("jailbroken").value(jailbroken);
        writer.name("id").value(id);
        writer.name("locale").value(locale);
        writer.name("manufacturer").value(manufacturer);
        writer.name("model").value(model);
        writer.name("osName").value(osName);
        writer.name("osVersion").value(osVersion);
        writer.name("runtimeVersions").value(runtimeVersions);
        writer.name("totalMemory").value(totalMemory);
    }
}
